package cn.yl.caner.dao;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import cn.yl.caner.utils.DataSourceUtils;
import cn.yl.caner.utils.MyDataSourceUtils;

public class CheckDao {
	
	public long check(String is,String index) throws SQLException{
		Connection con = MyDataSourceUtils.getCurrentConnection();
		DataSource source = DataSourceUtils.getDataSource();
		QueryRunner q = new QueryRunner(source);
//		String sql ="select count(*) from account where username = ?";
		String sql ="";
		long result = -1;
//		switch(index){
//			case "username":
//				 sql ="select count(*) from account where username = ?";
//				break;
//			case "email":
//				 sql ="select count(*) from account where email = ?";
//				 break;
//			case "phone":
//				 sql ="select count(*) from account where phone = ?";
//				 break;
//		}
		System.out.println(is+ "," +index);
		if(index.equals("username")){
			sql ="select count(*) from account where username = ?";
			 result = (Long) q.query(con,sql, new ScalarHandler(), is);
			return  result;
		}
		if(index.equals("email")){
			sql ="select count(*) from account where email = ?";
			 result = (Long) q.query(con,sql, new ScalarHandler(), is);
			return  result;
		}
		if(index.equals("phone")){
			sql ="select count(*) from account where phone = ?";
			 result = (Long) q.query(con,sql, new ScalarHandler(), is);
			return  result;
		}
		return result;
	}
	public long checkPassword (String password,String username) throws SQLException{
		Connection con = MyDataSourceUtils.getCurrentConnection();
		DataSource source = DataSourceUtils.getDataSource();
		QueryRunner q = new QueryRunner(source);
		long right = -1;
		String sql ="select password from account where username = ?";
		String repassword = (String) q.query(con,sql, new ScalarHandler(), username);
		System.out.println("DAO密码"+repassword);
		if(password.equals(repassword)) {
			right = 1;
			System.out.println(right);
			return right;
		}
		System.out.println("错误"+right);
		return right;
	}
}
	

